import pandas as pd

# 定义一个抽象方法，用于读取数据
class data_treat:
    def read_data(self):
        pass

# 定义一个具体类，用于读取csv(表格)文件
class csv_treat(data_treat):  # 继承抽象类
    def __init__(self, path):
        self.path = path

    def read_data(self):  # 重写data_treat中的抽象方法
        try:
            data = pd.read_csv(self.path)
            print("文件读取成功")
            return data
        except Exception as e:
            print("文件读取失败，错误信息为：", e)
            return None

# 定义一个具体类，用于读取xlsx(表格)文件
class xlsx_treat(data_treat):  # 继承抽象类
    def __init__(self, path):
        self.path = path

    def read_data(self):  # 重写data_treat中的抽象方法
        try:
            data = pd.read_excel(self.path)
            print("文件读取成功")
            return data
        except Exception as e:
            print("文件读取失败，错误信息为：", e)
            return None

# 测试代码
if __name__ == '__main__':
    # 使用xlsx_treat类来处理xlsx文件
    flight_data = xlsx_treat(r"D:\Python learn\pythonProject\python—learn\learning-python-xi\概率论代码\大数定律大作业\flight-data.xlsx")
    data = flight_data.read_data()
    if data is not None:
        print(data.to_string(justify='right'))
